package com.huawei.appmarket.component.feedback.activity;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.text.Editable;
import android.text.Selection;
import android.text.Spannable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.accessibility.AccessibilityEvent;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.Nullable;
import java.util.ArrayList;
import o.dfj.c;
import o.dfj.e;
import o.dfj.f;
import o.Logger;
import o.hc;
import o.in;
import o.km;

public class FeedBackCounterTextLayout extends LinearLayout {
    private boolean ʻ;
    private int ʽ;
    private d ˊ;
    private final ArrayList<View> ˋ;
    private TextView ˎ;
    private EditText ˏ;
    private int ॱ;
    private int ॱॱ;
    private int ᐝ;

    class a extends hc {
        final /* synthetic */ FeedBackCounterTextLayout ˊ;

        private a(FeedBackCounterTextLayout feedBackCounterTextLayout) {
            this.ˊ = feedBackCounterTextLayout;
        }

        public void ˎ(View view, AccessibilityEvent accessibilityEvent) {
            super.ˎ(view, accessibilityEvent);
            accessibilityEvent.setClassName(FeedBackCounterTextLayout.class.getSimpleName());
        }

        public void ˊ(View view, AccessibilityEvent accessibilityEvent) {
            super.ˊ(view, accessibilityEvent);
        }

        public void ˋ(View view, in inVar) {
            super.ˋ(view, inVar);
            inVar.ˏ(FeedBackCounterTextLayout.class.getSimpleName());
            if (this.ˊ.ˏ != null) {
                inVar.ˊ(this.ˊ.ˏ);
            }
            CharSequence text = this.ˊ.ˎ != null ? this.ˊ.ˎ.getText() : null;
            if (!TextUtils.isEmpty(text)) {
                inVar.ॱˊ(true);
                inVar.ᐝ(text);
            }
        }
    }

    public enum d {
        Bubble,
        Linear
    }

    public FeedBackCounterTextLayout(Context context) {
        this(context, null);
    }

    public FeedBackCounterTextLayout(Context context, AttributeSet attributeSet) {
        this(context, attributeSet, 0);
    }

    public FeedBackCounterTextLayout(Context context, AttributeSet attributeSet, int i) {
        super(context, attributeSet);
        this.ˋ = new ArrayList(1);
        this.ॱॱ = -1;
        this.ᐝ = -1;
        setOrientation(1);
        TypedArray obtainStyledAttributes = context.obtainStyledAttributes(attributeSet, f.ॱꓸ);
        try {
            this.ʽ = obtainStyledAttributes.getResourceId(f.ॱꜞ, 0);
            this.ॱ = context.getResources().getColor(o.dfj.d.ˊ);
            this.ʻ = obtainStyledAttributes.getBoolean(f.ॱᶥ, false);
            this.ˊ = d.values()[obtainStyledAttributes.getInt(f.ॱﾞ, d.Bubble.ordinal())];
        } catch (RuntimeException e) {
            Logger.e("CounterTextLayout", "CounterTextLayout error!");
        } finally {
            obtainStyledAttributes.recycle();
        }
        ˎ();
        if (km.getImportantForAccessibility(this) == 0) {
            km.setImportantForAccessibility(this, 1);
        }
        km.ˏ(this, new a());
    }

    public void addView(View view, int i, LayoutParams layoutParams) {
        if (view instanceof EditText) {
            ॱ((EditText) view);
            super.addView(view, 0, ˋ(layoutParams));
            return;
        }
        super.addView(view, i, layoutParams);
    }

    private void ॱ(EditText editText) {
        if (this.ˏ != null) {
            throw new IllegalArgumentException("We already have an EditText, can only have one");
        }
        this.ˏ = editText;
        this.ˏ.setImeOptions(this.ˏ.getImeOptions() | 33554432);
        if (this.ˊ == d.Bubble) {
            this.ˏ.setBackgroundResource(e.ˎ);
        }
        this.ˏ.addTextChangedListener(new TextWatcher(this) {
            final /* synthetic */ FeedBackCounterTextLayout ˋ;

            {
                this.ˋ = r1;
            }

            public void afterTextChanged(Editable editable) {
            }

            public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
            }

            public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
                if (-1 == this.ˋ.ॱॱ || -1 == this.ˋ.ᐝ) {
                    this.ˋ.ˋ(null);
                    return;
                }
                CharSequence text = this.ˋ.ˏ.getText();
                int length = text.length();
                if (length > this.ˋ.ॱॱ) {
                    length = Selection.getSelectionEnd(text);
                    this.ˋ.ˏ.setText(text.toString().substring(0, this.ˋ.ॱॱ));
                    Spannable text2 = this.ˋ.ˏ.getText();
                    if (length > text2.length()) {
                        length = text2.length();
                    }
                    Selection.setSelection(text2, length);
                    Animation loadAnimation = AnimationUtils.loadAnimation(this.ˋ.getContext(), c.ˋ);
                    if (loadAnimation != null) {
                        loadAnimation.setAnimationListener(new AnimationListener(this) {
                            final /* synthetic */ AnonymousClass2 ˎ;

                            {
                                this.ˎ = r1;
                            }

                            public void onAnimationStart(Animation animation) {
                                this.ˎ.ˋ.ˎ.setTextColor(this.ˎ.ˋ.ॱ);
                                this.ˎ.ˋ.ˎ(e.ˋ, ColorStateList.valueOf(this.ˎ.ˋ.ˎ.getCurrentTextColor()));
                            }

                            public void onAnimationRepeat(Animation animation) {
                            }

                            public void onAnimationEnd(Animation animation) {
                                this.ˎ.ˋ.ˎ(e.ˎ, null);
                                this.ˎ.ˋ.ˎ.setTextAppearance(this.ˎ.ˋ.getContext(), this.ˎ.ˋ.ʽ);
                            }
                        });
                        this.ˋ.ˏ.startAnimation(loadAnimation);
                    }
                } else if (length > this.ˋ.ᐝ) {
                    this.ˋ.ˋ(length + " / " + this.ˋ.ॱॱ);
                } else {
                    this.ˋ.ˋ(null);
                }
            }
        });
        if (this.ˎ != null) {
            this.ˎ.setPaddingRelative(this.ˏ.getPaddingStart(), 0, this.ˏ.getPaddingEnd(), 0);
        }
    }

    private LinearLayout.LayoutParams ˋ(LayoutParams layoutParams) {
        return layoutParams instanceof LinearLayout.LayoutParams ? (LinearLayout.LayoutParams) layoutParams : new LinearLayout.LayoutParams(layoutParams);
    }

    public void setHint(@Nullable CharSequence charSequence) {
        this.ˏ.setHint(charSequence);
        sendAccessibilityEvent(2048);
    }

    public void setMaxLength(int i) {
        this.ॱॱ = i;
        this.ᐝ = (this.ॱॱ * 9) / 10;
    }

    private void ˎ() {
        this.ˎ = new TextView(getContext());
        this.ˎ.setTextAppearance(getContext(), this.ʽ);
        this.ˎ.setVisibility(this.ʻ ? 4 : 8);
        ˏ();
        if (this.ˏ != null) {
            this.ˎ.setPaddingRelative(this.ˏ.getPaddingStart(), 0, this.ˏ.getPaddingEnd(), 0);
        }
    }

    private void ˏ() {
        LayoutParams layoutParams = new LinearLayout.LayoutParams(-2, -2);
        layoutParams.gravity = 8388693;
        addView(this.ˎ, layoutParams);
    }

    private void ˋ(@Nullable CharSequence charSequence) {
        if (!TextUtils.isEmpty(charSequence)) {
            this.ˎ.setAlpha(0.0f);
            this.ˎ.setText(charSequence);
            this.ˎ.animate().alpha(1.0f).setDuration(50).setInterpolator(new LinearInterpolator()).setListener(new AnimatorListenerAdapter(this) {
                final /* synthetic */ FeedBackCounterTextLayout ˋ;

                {
                    this.ˋ = r1;
                }

                public void onAnimationStart(Animator animator) {
                    this.ˋ.ˎ.setVisibility(0);
                }
            }).start();
            if (this.ˊ == d.Bubble) {
                this.ˏ.setBackgroundResource(e.ˎ);
            }
        } else if (this.ˎ.getVisibility() == 0) {
            this.ˎ.animate().alpha(0.0f).setDuration(50).setInterpolator(new LinearInterpolator()).setListener(new AnimatorListenerAdapter(this) {
                final /* synthetic */ FeedBackCounterTextLayout ॱ;

                {
                    this.ॱ = r1;
                }

                public void onAnimationEnd(Animator animator) {
                    this.ॱ.ˎ.setVisibility(this.ॱ.ʻ ? 4 : 8);
                }
            }).start();
            ˎ(e.ˎ, null);
        }
        sendAccessibilityEvent(2048);
    }

    private void ˎ(int i, ColorStateList colorStateList) {
        if (this.ˊ == d.Bubble) {
            this.ˏ.setBackgroundResource(i);
        } else if (this.ˊ == d.Linear) {
            this.ˏ.setBackgroundTintList(colorStateList);
        }
    }

    protected void onMeasure(int i, int i2) {
        if (this.ˊ == d.Bubble) {
            ˏ(i, i2);
        } else {
            super.onMeasure(i, i2);
        }
    }

    private void ˏ(int i, int i2) {
        Object obj;
        int max;
        int childCount = getChildCount();
        if (MeasureSpec.getMode(i) == 1073741824 && MeasureSpec.getMode(i2) == 1073741824) {
            obj = null;
        } else {
            obj = 1;
        }
        this.ˋ.clear();
        int i3 = 0;
        int i4 = 0;
        int i5 = 0;
        int i6 = 0;
        while (i6 < childCount) {
            int combineMeasuredStates;
            int i7;
            int i8;
            View childAt = getChildAt(i6);
            if (childAt.getVisibility() != 8) {
                measureChildWithMargins(childAt, i, 0, i2, 0);
                LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) childAt.getLayoutParams();
                int max2 = Math.max(i4, (childAt.getMeasuredWidth() + layoutParams.leftMargin) + layoutParams.rightMargin);
                max = Math.max(i3, (childAt.getMeasuredHeight() + layoutParams.topMargin) + layoutParams.bottomMargin);
                combineMeasuredStates = combineMeasuredStates(i5, childAt.getMeasuredState());
                if (obj != null && (layoutParams.width == -1 || layoutParams.height == -1)) {
                    this.ˋ.add(childAt);
                }
                i7 = combineMeasuredStates;
                i8 = max2;
                combineMeasuredStates = max;
            } else {
                i7 = i5;
                i8 = i4;
                combineMeasuredStates = i3;
            }
            i6++;
            i5 = i7;
            i4 = i8;
            i3 = combineMeasuredStates;
        }
        setMeasuredDimension(resolveSizeAndState(Math.max(i4, getSuggestedMinimumWidth()), i, i5), resolveSizeAndState(Math.max(i3, getSuggestedMinimumHeight()), i2, i5 << 16));
        max = this.ˋ.size();
        if (max > 1) {
            for (max2 = 0; max2 < max; max2++) {
                View view = (View) this.ˋ.get(max2);
                MarginLayoutParams marginLayoutParams = (MarginLayoutParams) view.getLayoutParams();
                if (marginLayoutParams.width == -1) {
                    combineMeasuredStates = MeasureSpec.makeMeasureSpec(Math.max(0, (getMeasuredWidth() - marginLayoutParams.leftMargin) - marginLayoutParams.rightMargin), 1073741824);
                } else {
                    combineMeasuredStates = getChildMeasureSpec(i, marginLayoutParams.leftMargin + marginLayoutParams.rightMargin, marginLayoutParams.width);
                }
                if (marginLayoutParams.height == -1) {
                    i8 = MeasureSpec.makeMeasureSpec(Math.max(0, (getMeasuredHeight() - marginLayoutParams.topMargin) - marginLayoutParams.bottomMargin), 1073741824);
                } else {
                    i8 = getChildMeasureSpec(i2, marginLayoutParams.topMargin + marginLayoutParams.bottomMargin, marginLayoutParams.height);
                }
                view.measure(combineMeasuredStates, i8);
            }
        }
    }

    protected void onLayout(boolean z, int i, int i2, int i3, int i4) {
        if (this.ˊ == d.Bubble) {
            ˏ(i, i2, i3, i4);
            this.ˎ.offsetTopAndBottom(-9);
            return;
        }
        super.onLayout(z, i, i2, i3, i4);
    }

    private void ˏ(int i, int i2, int i3, int i4) {
        int childCount = getChildCount();
        int i5 = i3 - i;
        int i6 = i4 - i2;
        for (int i7 = 0; i7 < childCount; i7++) {
            View childAt = getChildAt(i7);
            if (childAt.getVisibility() != 8) {
                int i8;
                LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) childAt.getLayoutParams();
                int measuredWidth = childAt.getMeasuredWidth();
                int measuredHeight = childAt.getMeasuredHeight();
                int i9 = layoutParams.gravity;
                if (i9 == -1) {
                    i9 = 8388693;
                }
                int i10 = i9 & 112;
                switch (Gravity.getAbsoluteGravity(i9, getLayoutDirection()) & 7) {
                    case 1:
                        i9 = (((((i5 + 0) - measuredWidth) / 2) + 0) + layoutParams.leftMargin) - layoutParams.rightMargin;
                        break;
                    case 5:
                        i9 = (i5 - measuredWidth) - layoutParams.rightMargin;
                        break;
                    default:
                        i9 = layoutParams.leftMargin + 0;
                        break;
                }
                switch (i10) {
                    case 16:
                        i8 = (((((i6 + 0) - measuredHeight) / 2) + 0) + layoutParams.topMargin) - layoutParams.bottomMargin;
                        break;
                    case 80:
                        i8 = (i6 - measuredHeight) - layoutParams.bottomMargin;
                        break;
                    default:
                        i8 = layoutParams.topMargin + 0;
                        break;
                }
                childAt.layout(i9, i8, measuredWidth + i9, measuredHeight + i8);
            }
        }
    }
}
